package org.lds.logger.config.appender.async;

import org.lds.logger.config.LifeCycle;
import org.lds.logger.config.appender.Appender;
import org.lds.logger.event.LoggerEvent;
import org.lds.logger.util.Utils;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public abstract class AbstractAsyncAppenderGroup implements AsyncAppenderGroup, LifeCycle {
    private int queueSize;
    private List<Appender> asyncAppenderList;

    private BlockingQueue<LoggerEvent> queue;

    public BlockingQueue<LoggerEvent> getQueue() {
        return queue;
    }

    @Override
    public int getQueueSize() {
        return queueSize;
    }

    @Override
    public void setQueueSize(int queueSize) {
        this.queueSize = queueSize;
    }

    @Override
    public List<Appender> getAsyncAppenderList() {
        return asyncAppenderList;
    }

    @Override
    public void setAsyncAppenderList(List<Appender> asyncAppenderList) {
        this.asyncAppenderList = asyncAppenderList;
    }

    @Override
    public void initialize() {
        this.queue = new ArrayBlockingQueue<>(getQueueSize());
    }

    @Override
    public void append(LoggerEvent loggerEvent) {
        try {
            queue.put(loggerEvent);
        } catch (Throwable e) {
            Utils.report("AsyncAppenderGroup append日志丢失. loggerEvent=" + loggerEvent);
        }
    }

}
